I've got a weird issue, possibly because I've customized some PHP on my Wordpress site.
I've got a template called homepage.php that displays all the posts from today's date (taken directly from the Wordpress Codex, filed under WP_Query).
About 7 or 8PM every day, all the posts disappear from that page. Why? And how do I fix it?
I thought at a certain point that it might be a post expiration issue, but I never set any expiration - I don't even think that's an included feature in Wordpress, is it?
Many thanks.
Full code included below just in case there's some clue here.
<?php
/**
* Template Name: Home Page
* The template for displaying all of today's posts on home page.
*
* This is the template that displays only today's posts.
* Please note that this is the WordPress construct of pages and that
* other "pages" on your WordPress site will use a different template.
*
* #package WordPress
* #subpackage Twenty_Fifteen
* #since Twenty Fifteen 1.0
*/
get_header(); ?>
<?php
$today = getdate();
$query = new WP_Query( 'year=' . $today['year'] . '&monthnum=' . $today['mon'] . '&day=' . $today['mday'] );
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
if ($query->have_posts() )
{
echo'<ul>';
while ( $query->have_posts() ){
$query->the_post();
echo '<li>' . get_template_part('content', get_post_format() ) . '</li>';
}
echo '</ul>';
/* Restore Post Data */
wp_reset_postdata();
//else : // no posts found.
}
?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>
Related
What is best practice for making a function call on a wordpress page?
For instance if you want to call my_special_function(); on the home page, where is the proper place to put the function call (ie home-page.php, /template-parts/content-page.php etc.).
<?php if( function_exists( my_special_function ) ) {
my_special_function();
} ?>
FYI Im using Underscores theme.
I've seen a few comments regarding shortcode. Would something like the below WP page template with the shortcode inserted be a best practice over just calling the function on that page?
So if I wanted the function to be called in the page template below I would just insert the shortcode wherever I want it on that page template or just is just calling the function is sufficient or best practice
<?php
/**
* Template Name: Home Page
*
* The template for displaying the home page.
*
* #link https://codex.wordpress.org/Template_Hierarchy
*
* #package Rollins_Ridge
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
// insert Shortcode
<?php echo do_shortcode('[special_function_shortcode]') ;?>
// or just call the function
my_special_function();
// or something else im not aware of
code here;
<?php
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; // End of the loop ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
Don't edit themes core files.
the proper way is using functions.php file in child theme directory.
you can add your function in this file and create shortocde using wordpress hook
like
my_special_function(){
//Your content goes here.
echo "This is proper way";
}
add_shortcode('special_function_shortcode','my_special_function');
and use following shortocde anywhere on site.
in wordpress page use [special_function_shortcode]
and in php use
<?php echo do_shortcode('[special_function_shortcode]') ;?>
I know this question is already asked a lot, but every time I read an answer on those questions it looks like I've implemented it like I should've. My problem is as follows:
I have added the following statement to my index.php to load a different header (saved in header-frontpage.php) for my static home page (called page-home.php).
if(is_front_page())
{
get_header('frontpage');
}
else
{
get_header();
}
I have also set page-home as my static front page in the wordpress settings->read.
Shouldn't this result in my homepage loading the header-frontpage.php? Now it just retreives my header.php for my frontpage as well. A live preview can be found at www.koencuijpers.com (wrong loading of header.php) and www.koencuijpers.com/spot-map (right loading of header.php).
My complete code for my index.php is:
<?php
/**
* The main template file.
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* E.g., it puts together the home page when no home.php file exists.
*
* #link https://codex.wordpress.org/Template_Hierarchy
*
* #package Spot_Utrecht
*/
if(is_front_page())
{
get_header('frontpage');
}
else
{
get_header();
}
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
if ( have_posts() ) :
if ( is_home() && ! is_front_page() ) : ?>
<header>
<h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
</header>
<?php
endif;
/* Start the Loop */
while ( have_posts() ) : the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
the_posts_navigation();
else :
get_template_part( 'template-parts/content', 'none' );
endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_sidebar();
get_footer();
?>
If you've set your homepage as a static page, then WordPress will be following its template hierarchy to decide what template to use:
https://developer.wordpress.org/themes/basics/template-hierarchy/#front-page-display
This means WordPress will not be using index.php as the template for your front page, so whatever changes you make there will have no effect.
Therefore you need to include your if statement in the appropriate template (probably front-page.php, depending on your theme setup).
Not sure whats wrong. I have looked and guess its php thats doing it. Ive seen this posted but cant find a fix.
its hard to post on here' * The template for displaying all pages
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages and that
* other 'pages' on your WordPress site will use a different template.
*
* #package WordPress
* #subpackage Twenty_Fourteen
* #since Twenty Fourteen 1.0
<?php
/**
* The template for displaying all pages
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages and that
* other 'pages' on your WordPress site will use a different template.
*
* #package WordPress
* #subpackage Twenty_Fourteen
* #since Twenty Fourteen 1.0
*/
<div id="main-content" class="main-content">
<?php
if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
// Include the featured content template.
get_template_part( 'featured-content' );
}
?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
endwhile;
?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar( 'content' ); ?>
</div><!-- #main-content -->
<?php
get_sidebar();
get_footer();
Close out the php tag on the last line of the initial block of commented out code. It should look like this:
<?php
/**
* The template for displaying all pages
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages and that
* other 'pages' on your WordPress site will use a different template.
*
* #package WordPress
* #subpackage Twenty_Fourteen
* #since Twenty Fourteen 1.0
*/ ?>
<div id="main-content" class="main-content">
I am building a personal website to host my university work, personal projects and photos etc.
The menu is a hierarchical structure made up of pages and links. Take my university pages for example. What I would like to achieve is to display posts that are related to the module code which is the page's slug.
I've used the following link http://codex.wordpress.org/Page_Templates#A_Page_of_Posts and managed to get it working but I have hard coded the module code into the template, meaning for each module I will have to have a separate template and the only thing that will be different from one file to the next is 5 characters which isn't great for code re-use.
What I am asking, is, is there a way to get the slug from the page I'm looking at and use that for the WP_Query arguments.
If you go to http://michaelnorris.co.uk/ and look at the menu structure. Navigate to University -> Year Three -> Individual Project, you will notice the url is http://michaelnorris.co.uk/uni/three/ci301 where ci301 is the module code for the Individual Project. I want to have this system on each of the module pages so that I can tag posts and they are displayed in the relevant module.
Ok, I actually found the answer myself, but for others looking to do the same. Below is a solution.
Solution found here on the Wordpress.org Codex http://codex.wordpress.org/Page_Templates#A_Page_of_Posts
Name the file pageofposts.php and edit the Page within the Wordpress Dashboard and set the Template (in the dropdown) to 'Page of Posts'. Bingo!
<?php
/*
Template Name: Page Of Posts
*/
/* This example is for a child theme of Twenty Thirteen:
* You'll need to adapt it the HTML structure of your own theme.
*/
get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
/* The loop: the_post retrieves the content
* of the new Page you created to list the posts,
* e.g., an intro describing the posts shown listed on this Page..
*/
global $post;
$slug = get_post( $post )->post_name;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Display content of page
get_template_part( 'content', get_post_format() );
wp_reset_postdata();
endwhile;
endif;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
// Change these category SLUGS to suit your use. category_name is comma separated.
'tag' => $slug,
'paged' => $paged
);
$list_of_posts = new WP_Query( $args );
?>
<?php if ( $list_of_posts->have_posts() ) : ?>
<?php /* The loop */ ?>
<?php while ( $list_of_posts->have_posts() ) : $list_of_posts->the_post(); ?>
<?php // Display content of posts ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentythirteen_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
I know this going to be very easy question for people with good PHP knowledge.
I have a WordPress website self hosted. I designed it for mobile devices and I would like to install Millennial Media advertisements in it. Millennial Media does not really provide detailed instructions and web resources strangely are not available!
I have knowledge in HTML,CSS and JavaScript but very poor in PHP :( .
any way I can do some changes PHP to achieve what I want, but I can't write them from zero and integrate them by my self.
Ok long story short
This the code provided by Millennial Media:
<?php
/*--------------------------------------------------------------*/
/* Millennial Media PHP Ad Coding, v.7.4.20 */
/* Copyright Millennial Media, Inc. 2006 */
/* */
/* The following code requires PHP >= 4.3.0 and */
/* allow_url_fopen 1 set in php.ini file. */
/* */
/* NOTE: */
/* It is recommended that you lower the default_socket_timeout */
/* value in the php.ini file to 5 seconds. */
/* This will prevent network connectivity from affecting */
/* page loading. */
/*--------------------------------------------------------------*/
/*------- Publisher Specific Section -------*/
$mm_placementid = 123456;
$mm_adserver = "ads.mp.mydas.mobi";
/* The default response will be echo'd on the page */
/* if no Ad is returned, so any valid WML/XHTML string */
/* is acceptable. */
$mm_default_response = "";
/*------------------------------------------*/
/*----------- BEGIN AD INITIALIZATION ----------*/
/*----- PLEASE DO NOT EDIT BELOW THIS LINE -----*/
$mm_id = "NONE";
$mm_ua = "NONE";
#$mm_ip = $_SERVER['REMOTE_ADDR'];
if (isset($_SERVER['HTTP_USER_AGENT'] )){
$mm_ua = $_SERVER['HTTP_USER_AGENT'];
}
if (isset($_SERVER['HTTP_X_UP_SUBNO'])) {
$mm_id = $_SERVER['HTTP_X_UP_SUBNO'];
} elseif (isset($_SERVER['HTTP_XID'])) {
$mm_id = $_SERVER['HTTP_XID'];
} elseif (isset($_SERVER['HTTP_CLIENTID'])) {
$mm_id = $_SERVER['HTTP_CLIENTID'];
} else {
$mm_id = $_SERVER['REMOTE_ADDR'];
}
$mm_url = "http://$mm_adserver/getAd.php5?apid=$mm_placementid&auid="
. urlencode($mm_id) . "&uip=" . urlencode($mm_ip) . "&ua="
. urlencode($mm_ua);
/*------------ END AD INITIALIZATION -----------*/
?>
<?php
/* Place this code block where you want the ad to appear */
/*------- Reusable Ad Call -------*/
#$mm_response = file_get_contents($mm_url);
echo $mm_response != FALSE ? $mm_response : $mm_default_response;
/*--------- End Ad Call ----------*/
?>
I want the Advertisement to appear in the footer area ( I will edit the footer.php in twenty eleven theme )
but I want to know where shall I put these pieces of codes in wordpress files or shall I create a new ones and what to name them?
would some please help me with this issue and provide me with the file's names that required to be edited and how would they look like in the end ?
Thanks
Anywhere in your page, where you want the ad to appear, let's say inside a <div>, use:
<div>
<?php include('thatfiletheysentyou.php'); ?>
</div>
This will make output of that file appear where include is.
EDIT: Complete rewrite:
This is content of footer.php. I just installed wordpress for you, took 3 minutes. I edited footer.php including <?php include('ads.php'); ?> (see below), which appeared in footer, as expected
<?php
/**
* The template for displaying the footer.
*
* Contains the closing of the id=main div and all content after
*
* #package WordPress
* #subpackage Twenty_Eleven
* #since Twenty Eleven 1.0
*/
?>
</div><!-- #main -->
<footer id="colophon" role="contentinfo">
<?php
/* A sidebar in the footer? Yep. You can can customize
* your footer with three columns of widgets.
*/
if ( ! is_404() )
get_sidebar( 'footer' );
?>
<div id="site-generator">
<div>
<?php include('ads.php'); ?>
</div>
<?php do_action( 'twentyeleven_credits' ); ?>
<?php printf( __( 'Proudly powered by %s', 'twentyeleven' ), 'WordPress' ); ?>
</div>
</footer><!-- #colophon -->
</div><!-- #page -->
<?php wp_footer(); ?>
</body>
</html>
Now if you got a file from the ad company, change ads.php above to this file name. If you got a code pasted in (i don't know, an e-mail, website) - create a file ads.php and paste whatever you got from them. Place that file in twentyeleven subfolder. My file looks like this:
<?php
echo 'HEHEHE!';
//instead of this just paste your code here
?>
and on the page it is like so:
1 http://www.spzozwolsztyn.internetdsl.pl/wpress.jpg
If you got tired of them remove include line from footer.php.