Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
129 views
in Q2A Core by

Hi, If your main blog is located in root folder (e.g: example.com) and your questions2answer/qna site is located in your subdirectory floder (e.g: example.com/qna), and you want to show the Questions in your main blog from the qna site, you can do this by using rss url.

I have created two version of this code, Only version will display all the recent questions, and the 2nd version will display the questions on your blog based on the category on which the post is listed. For example; If Your Post is Listed in Category "A", then the questions from the "A" category of your qna site will be display, If the post category is "B", then the questions from the "B" Category of your QNA site will be display. (For that you need to create same categories in your both Blog and Qna theme).

Here is the first code:


function fetch_questions_shortcode()
{
    // Fetch the RSS feed
    $rss_url = 'https://example.com/qna/feed/questions/biochemistry.rss';
    $rss = fetch_feed($rss_url);

    if (!is_wp_error($rss)) {
        $max_items = $rss->get_item_quantity(5); // Number of questions to display
        $rss_items = $rss->get_items(0, $max_items);
    }

    ob_start();
    if ($max_items == 0) {
        echo '<p>No questions found.</p>';
    } else {
        echo '<ul>';
        foreach ($rss_items as $item) {
            echo '<li>';
            echo '<a href="' . esc_url($item->get_permalink()) . '">' . esc_html($item->get_title()) . '</a>';
            echo '<p>' . esc_html($item->get_description()) . '</p>';
            echo '</li>';
        }
        echo '</ul>';
    }

    return ob_get_clean();
}

add_shortcode('fetch_questions', 'fetch_questions_shortcode');


2nd Code (Display questions based on your post category):

function fetch_questions_shortcode()

{

    // Get the current post's category

    $categories = get_the_category();

    $category_slugs = array();



    foreach ($categories as $category) {

        $category_slugs[] = $category->slug;

    }



    // Determine the RSS feed URL based on the category

    $rss_urls = array(

        'biochemistry' => 'https://example.com/qna/feed/questions/biochemistry.rss',

        'cell-biology' => 'https://example.com/qna/feed/questions/cell-biology.rss',

        // Add more category => RSS URL mappings as needed

    );



    $rss_url = '';

    foreach ($category_slugs as $slug) {

        if (isset($rss_urls[$slug])) {

            $rss_url = $rss_urls[$slug];

            break;

        }

    }



    // Fetch the RSS feed

    $rss = fetch_feed($rss_url);



    if (!is_wp_error($rss)) {

        $max_items = $rss->get_item_quantity(5); // Number of questions to display

        $rss_items = $rss->get_items(0, $max_items);

    }



    ob_start();

    if ($max_items == 0) {

        echo '<p>No questions found.</p>';

    } else {

        echo '<ul>';

        foreach ($rss_items as $item) {

            echo '<li>';

            echo '<a href="' . esc_url($item->get_permalink()) . '">' . esc_html($item->get_title()) . '</a>';

            echo '<p>' . esc_html($item->get_description()) . '</p>';

            echo '</li>';

        }

        echo '</ul>';

    }



    return ob_get_clean();

}




add_shortcode('fetch_questions', 'fetch_questions_shortcode');

Use this short code to display the questions list: [fetch_questions]

You need to paste this code in your function.php, or create a custome plugin in your wordpress theme and paste it.

Q2A version: Version 1.8.6

Please log in or register to answer this question.

...