I'm creating a news system for my website. What I have is a main page {index.php} (where all the articles are shown) and an article page (article.php)
What I'm having trouble with is having content selected on the article page.
When you click on the title on the index.php I want it to go to the article.php and show that articles information (currently achieved through get articleID)...
When you click the category of the article, I want it to go to article.php and show ALL the articles with the same category
How can I set up my code to accomplish this?
Currently I have for the article.php page
if( isset($_GET['category']) ){
$subject= $_GET['category'];
} else {
$subject= $_GET['id'];
}
$STH = $DBH->query('SELECT * FROM articles WHERE category="$subject"');
while($row = $STH->fetch()) {
echo $row['content'];
}
For the index.php page I call the category through this
echo '<a class="post-type" href="articles.php?category='.$row['category'].'">'. $row['category'] .'</a>';
Well You're missing a bit of thing here. Actually you need three pages, not two. One page is index.php which actually shows up all the latest content (articles). Other one page is to show single article (article.php), the third page is articles.php or category.php, this page will show the articles like index.php page but of particular category.
Note: You can manage with two pages as well but you'll have to manage your article.php for two different layouts, one is single article and other is article list like index.php page. So i suggest to have articles.php or category.php as a seperate page.
Related
I would need to reach a custom section by page id (or PageModel Object), but I don't find a way to get the FrontendTemplate. I would like to use it in a dropdown navigation to echo a custom section of the hovered (parent) page.
If somebody will search for, the answer is:
<?php
$articles = \ArticleModel::findPublishedByPidAndColumn($id, $column_name);
echo static::getArticle($articles[0]); // for ex. the first article
?>
I have created a news dispatches post category. I also created a news releases page, this is the page where I want to show my news dispatches post category posts. How can I show my news dispatches posts in news releases page using post ID or using page permalink in a query? Here is my example code.
add_action('pre_get_posts', 'ad_filter_categories');
function ad_filter_categories($query) {
if ($query->is_main_query() && is_home()) {
$query->set('category_name','news dispatches');
}
}
How can I replace the is_home code line with the page/post ID or the page permalink? Thanks for the help.
Can't use is_page in pre_get_posts (see http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts).
So in the news releases page, add this in:
// Modify the page query
query_posts( 'category_name=news-dispatches');
// The Loop
if ( have_posts() ) {
echo '<ul>';
while ( $have_posts() ) {
the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
UPDATE:
To answer this question: "What php file I am gonna look into to insert this code? ":
It's hard to give exact instructions because it really depends on how your theme is setup.
You need to create a page called page-news-releases.php. See http://codex.wordpress.org/images/1/18/Template_Hierarchy.png for an explanation of page templates. So when wordpress shows the page with the slug news-releases, it will use this file instead of the default.
You'll put my code in this new page for the main loop. You'll also need to look at your other theme files and copy code that needs to go before and after it (for your header and footer and that sort of stuff). It's a little out of the scope of stackoverflow to teach wordpress template design, but hopefully this will get you started. Good luck!
I have a page named "Shop". On that page I display a custom menu, which contains links to categories like "Books", "Shoes" etc. Whenever I click one of those links/categories it takes me to a relevant category page, for example /category/books/.
I do not want it to redirect me to a category page, I want it to display posts on the same page ("Shop") that the menu item was clicked on. The only problem I have encountered trying to accomplish this, is the fact that I do not know how to change the custom menu behavior. I don't want it to redirect me, but instead send a GET value to the same page ("Shop"). Then the shop page would take that GET value and display relevant posts.
I know how to display posts from a category etc. I just don't know how to change the behavior of the custom menu.
Could anybody help me? I would be grateful.
Original Answer:
Use wp_get_nav_menu_items:
$menu_slug = 'YOUR_MENU_SLUG';
$menu_id = get_nav_menu_locations()[$menu_slug];
$menu_items = wp_get_nav_menu_items($menu_id);
foreach($menu_items as $item){
if($item->object == 'category'){
print('<p>Title: ' . $item->title . '<br>ID: ' . $item->object_id . '</p>');
}
}
wp_get_nav_menu_items returns an array of menu items. You get several information on the item, like type (post, category, ...), id, title and so on.
See http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items for a full list of properties.
Hope this helps :)
Update:
Assuming your permalink setting is set to default, the following code will print a modified menu that uses GET.
$menu = wp_nav_menu(array(
'theme_location'=>'YOUR_MENU_LOCATION',
'echo'=>0,
));
$new_url = $_SERVER['SCRIPT_NAME'] . '?shop_page=$1';
$menu = preg_replace('/href="[^"]*cat=(.+)"/', 'href="'.$new_url.'"', $menu);
print($menu);
The regex may not be the best as it ignores other GET values and I'm not experienced on them. If your permalink setting is different and every time you change it, you will have to edit the regex.
I need to change some positions on the Joomla template based on the article loaded.
Ex: I need to use different set of positions for a home page and different set of positions for inner pages. There are some common positions as well (Menu, Header, Footer,etc...).
Okay this is a bit tricky, you can get the current page name (menu name) and then make if statements for each "page" you made in the template source, i'll give an example ... Let's say you have a contact page and you want to add a position to it if the user is on the contact page ...
<?php
$currentpage = JSite::getMenu()->getActive()->name ;
// in joomla 2.5
$currentpage = JSite::getMenu()->getActive()->title;
if($currentpage == "Contact"){
echo '<jdoc:include type="modules" name="Contact" />';
}
?>
And so you have to workout your whole template and anticipate for each page, this was a simple example and it's up to you to expand it ...
I currently have a script that outputs a secondary navigation based on the site menus, however I can't think of anyway to link a post to a page so that the posts can still show a secondary navigation, is this even possible? Here is the current code i'm using to output my secondary navigation:
<?php
$secondAncestor = count($post->ancestors) -1; //figure out what level of navigation we are on, subtract one because we don't want to consider the top-level
if($post->post_parent!=0) //if the page is not a top-level category
{
echo '<nav><h2 class="widgettitle">In this section:</h2><ul class="secondary-nav"><li class="sidebarlist">';
//the following lists children of second level ancestor of the current page.
wp_list_pages("title_li=&child_of=".$post->ancestors[$secondAncestor]."& sort_column=menu_order&echo=1");
echo '</li>';
}
else //if the page is a top-level category
{
//listing only the child pages of the current section
$children= wp_list_pages("title_li=&child_of=".$post->ID."& sort_column=menu_order&echo=0");
if($children) //this will stop it from displaying a section heading if there are no elements in the section (for example on the home page)
{
echo '<nav><h2 class="widgettitle">In this section:</h2><ul class="secondary-nav"><li>';
echo $children;
echo '</li>';
}
}
echo '</ul></nav>';
?>
Pages are heirarchical, so they can have parents and children.
Posts are flat, and related by categories and tags.
In order to relate a post to a page, I think you're going to need to use a custom field on your posts. You could call it Parent Page ID, and then in your sidebar code, add a custom query that checks for posts whose Parent Page ID = the current page's id.
Check out Flexi Pages Widget. This will add a highly configurable subpage menu to your sidebar, which you can embed on posts & pages.