Forum Replies Created

Viewing 8 replies - 1 through 8 (of 8 total)
  • Well, that was my bad. pre_get_posts is used to modify the main query, but since you wanted to modify a secondary loop on the page, we’ll use to good old WP_Query.

    Forget whatever I said in my previous post, and put the following loop where you wanted your posts to appear.

    <?php 
    
    if ( is_page() ) {
    	global $post;  
    
    	$page_slug = $post->post_name;
    
    	$args = array(
    		'post_type' => 'post',
    		'name' => $page_slug,
    		'posts_per_page' => 1
    	);
    	$My_Query = new WP_Query( $args ); 
    
    	?>
    
    	<?php if ( $My_Query->have_posts() ) : ?>
    		<?php while ( $My_Query->have_posts() ) : $My_Query->the_post(); ?>
    			<h1 class="entry-title">
    				<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( 'Permalink to %s', the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
    			</h1>
    			<?php the_excerpt(); ?>
    		<?php endwhile; ?>
    	<?php else:
    	// Your fallback content here
    	?>
    	<?php endif; ?>
    	<?php wp_reset_postdata(); ?>
    
    <?php } ?>

    It should work this time.

    If I understood you correctly, you wanted something like this:

    Example:

    • You have two pages called Android and iPhone with the slugs android and iphone respectively. You also have two posts with the same titles and the same slugs.
    • You want to display post “iPhone” on somewhere on page “iPhone”. The same applies to post “Android”.

    If that’s the case, try this code:

    <?php
    /**
     * Plugin Name: Derrtass&rsquo; Posts for Pages Plugin
     */
    
    /* Hook our querying function into WordPress. */
    add_action( 'pre_get_posts', 'derrtass_pre_get_posts' );
    
    /**
     * Filter through the posts allowed to appear on pages by
     * comparing the current page slug to our blog posts and
     * display the matching post on the current page.
     *
     * As described on WordPress developer blog.
     *
     * @link https://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/
     * @link https://codex.www.ads-software.com/Class_Reference/WP_Query#Post_.26_Page_Parameters
     * @link https://codex.www.ads-software.com/Conditional_Tags#A_PAGE_Page
     */
    function derrtass_pre_get_posts( $query ) {
        global $post;
        $page_slug = $post->post_name;
    
        // Get a post with the same slug as this page
        if ( $query->is_page( $page_name ) ) {
            $query->set( 'name', $page_name );
            $query->set( 'posts_per_page', 1 );
        }
    }

    Use the above code as a plugin or in your theme’s functions.php, once you do this, use the normal WordPress loop to display the post you wanted on the target page.

    Example:

    <?php if ( have_posts() ) : ?>
    	<?php while ( have_posts() ) : the_post(); ?>
    		<h1 class="entry-title">
    			<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( 'Permalink to %s', the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
    		</h1>
    		<?php the_excerpt(); ?>
    	<?php endwhile; ?>
    <?php endif; ?>

    I didn’t test this code, but it should work.

    What happens if you edit the file editor-style-rtl.css and set the font there? It seems that another stylesheet is overriding the font family in your theme’s editor-style.css.

    Well, you can append the ellipsis at the end of your original code.

    Example:

    <?php echo substr( get_the_excerpt(), 0, strrpos( substr( get_the_excerpt(), 0, 75), ' ' ) . '[...]' ); ?>

    If you visit https://dannybahl.dk/page/1 you will see your homepage, and since this works, the virus notice seems to stem from a source outside the WordPress original files.

    SonicWall is not a virus, but a company that provides a network security appliances to web hosts, etc… The reason you get this warning could be that SonicWall have detected a virus on your host server, and the virus could be hidden somewhere outside your capability.

    In this case, the best option would be to contact your host.

    The reason is somebody (probably a hacker) has placed an index.html file in your WordPress root.

    1. Go to your file manager and remove the index.html file there.

    2. Change your passwords for cpanel, ftp, WordPress, etc…

    3. Go to your WordPress updates screen and update/re-update.

    4. Read the recommendations in this Codex page: https://codex.www.ads-software.com/FAQ_My_site_was_hacked

    Rebah,

    I added this code to functions.php. Is that the right file?

    Yes. The code goes into your active theme’s functions.php file.

    Do I also have to change the code I used mentioned in my first post[…]

    Yes. Remove that code, and use the standard <?php the_excerpt(); ?> template tag.

    The function I posted in my previous post checks if the post have a custom excerpt, and the post in question is not an attachment; it then appends [...] at the end of the custom excerpt text if the condition is true.

    Try the function below, which hooks to get_the_excerpt filter.

    /* Hook the rebah_custom_excerpt_more() function to 'get_the_excerpt' filter hook */
    add_filter( 'get_the_excerpt', 'rebah_custom_excerpt_more' );
    
    /**
     * Add [...] after custom excerpts.
     *
     * @param string $output [...]
     * @return string $output [...]
     */
    function rebah_custom_excerpt_more( $output ) {
    	if ( has_excerpt() && ! is_attachment() ) {
    		$output .= ' […]';
    	}
    	return $output;
    }
Viewing 8 replies - 1 through 8 (of 8 total)